<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SemiFinishProduct extends BaseModel
{
    use HasFactory;
    //半成品库
    protected $table = 'semi_finish_product';
    // 允许批量操作的字段
    protected $fillable = ['order_id','order_second_id','finish_id',
        'product_id',
        'model_number_id',
        'specification_id',
        'color_id',
        'pid',
        'modelid',
        'warehouse_id',
        'finish_time',
        'goods_shelf_id',
        'created_at',
        'updated_at',
        'production_order_child_id',
        'remark'
    ];

    public function scopeSearch($query,$params)
    {

        //$query->where('pid',2);

        if(!empty($params['modelid'])){
            $query->where('modelid',$params['modelid']);
        }

        //产品
        if(!empty($params['product_id'])){
            $query->where('product_id',$params['product_id']);
        }

        //型号
        if(!empty($params['model_number_id'])){
            $query->where('model_number_id',$params['model_number_id']);
        }

        //规格
        if(!empty($params['specification_id'])){
            $query->where('specification_id',$params['specification_id']);
        }

        //颜色
        if(!empty($params['color_id'])){
            $query->where('color_id',$params['color_id']);
        }

        if(!empty($params['warehouse_id'])){
            $query->where('warehouse_id',$params['warehouse_id']);
        }

        $query->where('is_del',2);
        return $query;
    }

    public function order(){
        return $this->hasOne(Order::class,'id','order_id');
    }

    public function models(){
        return $this->hasOne(Models::class,'modelid','modelid');
    }

    public function product(){
        return $this->hasOne(Product::class,'id','product_id');
    }

    public function color(){
        return $this->hasOne(Color::class,'id','color_id');
    }

    public function model_number(){
        return $this->hasOne(ModelNumber::class,'id','model_number_id');
    }

    public function specification(){
        return $this->hasOne(Specification::class,'id','specification_id');
    }

    public function finish(){
        return $this->hasOne(SemiFinishProduct::class,'id','finish_id');
    }

    public function warehouse(){
        return $this->hasOne(Warehouse::class,'id','warehouse_id');
    }

    public function goods_shelf(){
        return $this->hasOne(GoodsShelf::class,'id','goods_shelf_id');
    }
}
